﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.ShprossesBuilder.Modelling;
using ITAP.ShprossesBuilder.Calculations;
using ITAP.ShprossesBuilder.Editor;
using ITAP.ShprossesBuilder.Properties;
using ITAP.Core.Modelling;
using ITAP.Core.Calculations;

namespace ITAP.ShprossesBuilder.Operations
{
    public class OperationSetDistanceBetweenShprossEnds : IOperation
    {
        #region IOperation Members

        public string Name
        {
            get { return "Задать расстояние между концами профиля"; }
        }

        public OperationGroup OperationGroup
        {
            get { return OperationGroup.ShprossOperation; }
        }

        public System.Drawing.Image Thumbnail
        {
            get { return Resources.ShprossSetDistanceBetweenEnds.ToBitmap(); }
        }

        public void Execute(ModelControl modelControl)
        {
            SetDistanceBetweenShprossEndsForm distanceForm = new SetDistanceBetweenShprossEndsForm();
            if (distanceForm.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                return;

            double distance = distanceForm.Distance;
            foreach (IComponent selectedComponent in modelControl.ModelContainer.SelectedComponentsStack)
            {
                if (selectedComponent is Shpross)
                {
                    Shpross shpross = selectedComponent as Shpross;
                    if (shpross.SelectedPoint == ShprossPoints.Start)
                    {
                        ModelVector vector = new ModelVector(shpross.EndPoint, shpross.StartPoint);
                        vector.Length = distance;
                        ModelPoint newStartPoint = ModelCalculations.GetOffsetPoint(shpross.EndPoint, vector);
                        shpross.MoveStartPointTo(newStartPoint, shpross.Parent.BoundingBox, true);
                    }
                    else if (shpross.SelectedPoint == ShprossPoints.End)
                    {
                        ModelVector vector = new ModelVector(shpross.StartPoint, shpross.EndPoint);
                        vector.Length = distance;
                        ModelPoint newEndPoint = ModelCalculations.GetOffsetPoint(shpross.StartPoint, vector);
                        shpross.MoveEndPointTo(newEndPoint, shpross.Parent.BoundingBox, true);
                    }
                    else if (shpross.SelectedPoint == ShprossPoints.Center)
                    {
                        ModelPoint centerPoint = new ModelPoint((shpross.StartPoint.X + shpross.EndPoint.X) / 2,
                                                                (shpross.StartPoint.Y + shpross.EndPoint.Y) / 2);

                        ModelVector vector1 = new ModelVector(centerPoint, shpross.StartPoint);
                        ModelVector vector2 = new ModelVector(centerPoint, shpross.EndPoint);
                        vector1.Length = distance / 2;
                        vector2.Length = distance / 2;
                        ModelPoint newStartPoint = ModelCalculations.GetOffsetPoint(centerPoint, vector1);
                        ModelPoint newEndPoint = ModelCalculations.GetOffsetPoint(centerPoint, vector2);
                        shpross.MoveStartPointTo(newStartPoint, shpross.Parent.BoundingBox, true);
                        shpross.MoveEndPointTo(newEndPoint, shpross.Parent.BoundingBox, true);
                    }
                }
            }
        }

        public bool CanUndo
        {
            get { return true; }
        }

        #endregion
    }
}
